<html>

  <head><title>BeforeUnload dialog</title>
  <script>
    function clickLink() {
      var evt = document.createEvent("MouseEvents");
      evt.initMouseEvent("click", true, true, window, 0, 0, 0, 0, 0, false, false,
                         false, false, 0, null);
      var target = document.getElementById("link");
      target.dispatchEvent(evt);
    }

    function clickLinkSoon() {
      window.setTimeout(clickLink, 100);
      return true;
    }

    window.addEventListener("beforeunload", function(e) {
      var confirmationMessage = "foo";
      e.returnValue = confirmationMessage;
      return confirmationMessage;
    });

  </script>
  </head>

<a href="title2.html" id="link">link</a><br>

</html>
